<?php
class buy_items
{
	var $ipsclass;
	
	var $order = array(
							'item' => 'item_id',
							'icon' => 'icon',
							'name' => 'item_name',
							'category' => '',
							'stock' => '',
							'resell' => 'resell',
							'price' => 'price',
							'tax' => 'total_tax',
						);
	var $order_by = array(
							'ASC' => 1,
							'DESC' => -1,
						);
	
	function auto_run()
	{
		$redirect = array('checkout'=>'checkout','buy'=>'buying_items');
		
		if(!isset($redirect[$this->ipsclass->input['CODE']]))
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		// Validate the input
		if(!is_array($this->ipsclass->input['item']) || count($this->ipsclass->input['item']) == 0)
		{
			$this->ipsclass->boink_it($this->ipsclass->market_class->base_url."CODE=shop");
		}
		
		$function = $redirect[$this->ipsclass->input['CODE']];
		$this->$function();
	}
	
	function checkout()
	{
		$items = array();
		$price = array();
		$price['subtotal'] = 0;
		$price['tax'] = 0;
		$price['total'] = 0;
		$price['quanity'] = 0;
		foreach($this->ipsclass->input['item'] as $item_id => $quanity)
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item_id]))
			{
				continue;
			}
			if(in_array($this->ipsclass->member['mgroup'],$this->ipsclass->cache['market_items'][$item_id]['limit_buy']))
			{
				continue;
			}
			$quanity = $this->ipsclass->numbers->parse_input_number($quanity,'int');
			if($this->ipsclass->cache['market_items'][$item_id]['stock'] >= 0 && $this->ipsclass->numbers->compare($quanity,$this->ipsclass->cache['market_items'][$item_id]['stock']) > 0)
			{
				$quanity = $this->ipsclass->cache['market_items'][$item_id]['stock'];
			}
			if($this->ipsclass->cache['market_items'][$item_id]['inventory_limit'] && $this->ipsclass->numbers->compare($quanity,$this->ipsclass->cache['market_items'][$item_id]['inventory_limit']) > 0)
			{
				$quanity = $this->ipsclass->cache['market_items'][$item_id]['inventory_limit'];
			}
			$this->ipsclass->input['item'][$item_id] = $quanity;
			if($this->ipsclass->numbers->compare($quanity,0) <= 0 || $this->ipsclass->cache['market_items'][$item_id]['hide_item'])
			{
				continue;
			}
			if($this->ipsclass->cache['market_items'][$item_id]['inventory_limit'] && $this->ipsclass->numbers->compare($this->ipsclass->numbers->add($quanity,count($this->ipsclass->market_class->inventory[$item_id])),$this->ipsclass->cache['market_items'][$item_id]['inventory_limit']) > 0)
			{
				continue;
			}
			
			$this->ipsclass->cache['market_items'][$item_id]['fprice'] = $this->ipsclass->numbers->format_currency($this->ipsclass->cache['market_items'][$item_id]['price']);
			$this->ipsclass->cache['market_items'][$item_id]['ftotal_tax'] = $this->ipsclass->numbers->do_number_format($this->ipsclass->cache['market_items'][$item_id]['total_tax'])."%";
			
			$this->ipsclass->cache['market_items'][$item_id]['subtotal'] = $this->ipsclass->numbers->multiply($this->ipsclass->cache['market_items'][$item_id]['price'],$quanity);
			$this->ipsclass->cache['market_items'][$item_id]['fsubtotal'] = $this->ipsclass->numbers->format_currency($this->ipsclass->cache['market_items'][$item_id]['subtotal']);
			
			$this->ipsclass->cache['market_items'][$item_id]['tax_amount'] = $this->ipsclass->numbers->divide($this->ipsclass->numbers->multiply($quanity,$this->ipsclass->cache['market_items'][$item_id]['price'],$this->ipsclass->cache['market_items'][$item_id]['total_tax']),100);
			$this->ipsclass->cache['market_items'][$item_id]['ftax_amount'] = $this->ipsclass->numbers->format_currency($this->ipsclass->cache['market_items'][$item_id]['tax_amount']);
			
			$this->ipsclass->cache['market_items'][$item_id]['buying'] = $this->ipsclass->do_number_format($quanity);
			
			$this->ipsclass->cache['market_items'][$item_id]['description'] = $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item_id]->parse_store_description()));
			
			$price['quanity'] = $this->ipsclass->numbers->add($price['quanity'],$quanity);
			
			$price['subtotal'] = $this->ipsclass->numbers->add($price['subtotal'],$this->ipsclass->cache['market_items'][$item_id]['subtotal']);
			$price['tax'] = $this->ipsclass->numbers->add($price['tax'],$this->ipsclass->cache['market_items'][$item_id]['tax_amount']);
			
			$items[$item_id] = $item_id;
		}
		
		if(count($items) == 0)
		{
			$this->ipsclass->market_class->redirect("","CODE=shop&category=".$this->ipsclass->input['category']."&all_items=".$this->ipsclass->input['all_items']."&sort=".$this->ipsclass->input['sort']."&by=".$this->ipsclass->input['by'],array('instant'=>1));
		}
		
		uksort($items, array(&$this, 'order'));
		
		$price['total'] = $this->ipsclass->numbers->add($price['subtotal'],$price['tax']);
		
		$price['tax'] = $this->ipsclass->numbers->format_currency($price['tax']);
		$price['subtotal'] = $this->ipsclass->numbers->format_currency($price['subtotal']);
		$price['ftotal'] = $this->ipsclass->numbers->format_currency($price['total']);
		
		$this->ipsclass->output = $this->ipsclass->compiled_templates['skin_market']->checkout($items,$price);
		
		$this->ipsclass->nav[] = $this->ipsclass->lang['checkout'];
	}
	
	function buying_items()
	{
		$total_tax = 0;
		$price = 0;
		$tquanity = 0;
		foreach($this->ipsclass->input['item'] as $item_id => $quanity)
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item_id]) || in_array($this->ipsclass->member['mgroup'],$this->ipsclass->cache['market_items'][$item_id]['limit_buy']))
			{
				unset($this->ipsclass->input['item'][$item_id]);
				continue;
			}
			$quanity = $this->ipsclass->numbers->parse_input_number($quanity,'int');
			if($this->ipsclass->cache['market_items'][$item_id]['stock'] >= 0 && $this->ipsclass->numbers->compare($quanity,$this->ipsclass->cache['market_items'][$item_id]['stock']) > 0)
			{
				$quanity = $this->ipsclass->cache['market_items'][$item_id]['stock'];
			}
			if($this->ipsclass->cache['market_items'][$item_id]['inventory_limit'] && $this->ipsclass->numbers->compare($quanity,$this->ipsclass->cache['market_items'][$item_id]['inventory_limit']) > 0)
			{
				$quanity = $this->ipsclass->cache['market_items'][$item_id]['inventory_limit'];
			}
			$this->ipsclass->input['item'][$item_id] = $quanity;
			if($this->ipsclass->numbers->compare($quanity,0) <= 0 || $this->ipsclass->cache['market_items'][$item_id]['hide_item'])
			{
				unset($this->ipsclass->input['item'][$item_id]);
				continue;
			}
			if($this->ipsclass->cache['market_items'][$item_id]['inventory_limit'] && $this->ipsclass->numbers->compare($this->ipsclass->numbers->add($quanity,count($this->ipsclass->market_class->inventory[$item_id])),$this->ipsclass->cache['market_items'][$item_id]['inventory_limit']) > 0)
			{
				unset($this->ipsclass->input['item'][$item_id]);
				continue;
			}
			
			$total_tax = $this->ipsclass->numbers->add($total_tax,$this->ipsclass->numbers->divide($this->ipsclass->numbers->multiply($quanity,$this->ipsclass->cache['market_items'][$item_id]['price'],$this->ipsclass->cache['market_items'][$item_id]['total_tax']),100));
			$subprice = $this->ipsclass->numbers->round($this->ipsclass->numbers->divide($this->ipsclass->numbers->multiply($quanity,$this->ipsclass->cache['market_items'][$item_id]['price'],$this->ipsclass->cache['market_items'][$item_id]['total_tax'] + 100),100));
			$price = $this->ipsclass->numbers->add($price,$subprice);
			$tquanity = $this->ipsclass->numbers->add($tquanity,$quanity);
		}
		
		if(count($this->ipsclass->input['item']) == 0)
		{
			$this->ipsclass->market_class->redirect("","CODE=shop&category=".$this->ipsclass->input['category']."&all_items=".$this->ipsclass->input['all_items']."&sort=".$this->ipsclass->input['sort']."&by=".$this->ipsclass->input['by'],array('instant'=>1));
		}
		
		if($this->ipsclass->numbers->compare($price,$this->ipsclass->member[$this->ipsclass->vars['market_members_field']]) > 0)
		{
			$this->ipclass->Error(array('MSG'=>'not_enough_money'));
		}
		if($this->ipsclass->vars['market_inventory_max'] and $this->ipsclass->numbers->compare($this->ipsclass->numbers->add(count($this->ipsclass->market_class->inventory_id),$tquanity),$this->ipsclass->vars['market_inventory_max']) > 0)
		{
			$this->ipsclass->Error(array('MSG'=>'too_many_items'));
		}
		
		$time = time();
		
		foreach($this->ipsclass->input['item'] as $item_id => $quanity)
		{
			$count = 0;
			
			$expired = $this->ipsclass->cache['market_items'][$item_id]['expire_time'] > 0 ? $time + $this->ipsclass->cache['market_items'][$item_id]['expire_time'] * $this->ipsclass->cache['market_items'][$item_id]['expire_unit'] : 0;
			
			// Give the items
			for($i = 0; $i < $quanity; $i++)
			{
				$inv_item = array(
									'member_id' => $this->ipsclass->member['id'],
									'item' => $item_id,
									'price_bought' => $this->ipsclass->cache['market_items'][$item_id]['price'],
									'resell_price' => $this->ipsclass->cache['market_items'][$item_id]['resell'],
									'time_bought' => $time,
									'settings' => '',
									'expired' => $expired,
								);
				if($this->ipsclass->market_class->items_loaded[$item_id]->public_on_create_item($inv_item))
				{
					$this->ipsclass->DB->do_insert('market_inventory',$inv_item);
					$count++;
				}
			}
			$quanity = $count;
			
			if($quanity == 0)
			{
				continue;
			}
			
			// Update the item amount
			if($this->ipsclass->cache['market_items'][$item_id]['stock'] > 0)
			{
				$this->ipsclass->cache['market_items'][$item_id]['stock'] = $this->ipsclass->numbers->subtract($this->ipsclass->cache['market_items'][$item_id]['stock'],$count);
				if($this->ipsclass->cache['market_items'][$item_id]['restock'] == 1 || $this->ipsclass->cache['market_items'][$item_id]['restock'] == 2)
				{
					if(!$this->ipsclass->cache['market_items'][$item_id]['restock_time'] && $this->ipsclass->numbers->compare($this->ipsclass->cache['market_items'][$item_id]['stock'],$this->ipsclass->cache['market_items'][$item_id]['restock_when']) <= 0)
					{
						$this->ipsclass->cache['market_items'][$item_id]['restock_time'] = $this->ipsclass->market_class->next_restock_time($item_id);
					}
					else
					{
						$this->ipsclass->cache['market_items'][$item_id]['restock_time'] = 0;
					}
				}
			}
			$this->ipsclass->DB->do_update('market_items',array(
																	'stock' => $this->ipsclass->cache['market_items'][$item_id]['stock'],
																	'restock_time' => $this->ipsclass->cache['market_items'][$item_id]['restock_time'],
											),"item_id=".$item_id);
			
			// Need to log it
			$this->ipsclass->DB->do_insert('market_buy_logs',array(
																	'member_id' => $this->ipsclass->member['id'],
																	'method' => 'buy',
																	'item_id' => $item_id,
																	'ip_address' => $this->ipsclass->ip_address,
																	'time_bought' => $time,
																	'quanity' => $quanity,
																	'price_bought' => $this->ipsclass->cache['market_items'][$item_id]['price'],
																	'resell_price' => $this->ipsclass->cache['market_items'][$item_id]['resell'],
																	'tax_total' => $this->ipsclass->cache['market_items'][$item_id]['tax_amount'],
																	'category' => $this->ipsclass->cache['market_items'][$item_id]['category'],
																	'item_name' => $this->ipsclass->cache['market_items'][$item_id]['item_name'],
																	'item_description' => $this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item_id]->parse_store_description())),
																	'icon' => $this->ipsclass->cache['market_items'][$item_id]['icon'],
																	'category_name' => $this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item_id]['category']]['cat_name'],
										)						);
		}
		
		$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$price)
					),"id=".$this->ipsclass->member['id']);
		
		$this->ipsclass->market_class->record_fee($total_tax);
		
		$this->ipsclass->market_class->cache_items();
		
		$this->ipsclass->market_class->redirect('items_bought');
	}
	
	function order($a,$b)
	{
		if($this->order[$this->ipsclass->input['order']] != '')
		{
			$a_compare = $this->ipsclass->cache['market_items'][$a][$this->order[$this->ipsclass->input['order']]];
			$b_compare = $this->ipsclass->cache['market_items'][$b][$this->order[$this->ipsclass->input['order']]];
			if(is_numeric($a_compare) && is_numeric($b_compare))
			{
				if($a_compare > $b_compare)
				{
					return 1 * $this->order_by[$this->ipsclass->input['by']];
				}
				elseif($a_compare < $b_compare)
				{
					return -1 * $this->order_by[$this->ipsclass->input['by']];
				}
				else
				{
					return 0;
				}
			}
			else
			{
				return strcasecmp($a_compare,$b_compare) * $this->order_by[$this->ipsclass->input['by']];
			}
		}
		else
		{
			if($this->ipsclass->input['order'] == 'stock')
			{
				$a_2 = $this->ipsclass->input[$a];
				$b_2 = $this->ipsclass->input[$b];
				if($a_2 > $b_2)
				{
					return 1 * $this->order_by[$this->ipsclass->input['by']];
				}
				elseif($a_2 < $b_2)
				{
					return -1 * $this->order_by[$this->ipsclass->input['by']];
				}
				else
				{
					return 0;
				}
			}
			else
			{
				$a_2 = $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$a]['category']];
				$b_2 = $this->ipsclass->market_class->category_order[$this->ipsclass->cache['market_items'][$b]['category']];
				if($a_2 < $b_2)
				{
					return 1 * $this->order_by[$this->ipsclass->input['by']];
				}
				elseif($a_2 > $b_2)
				{
					return -1 * $this->order_by[$this->ipsclass->input['by']];
				}
				else
				{
					return 0;
				}
			}
		}
	}
}
?>